import {
  crmMessagNumAPI
} from '/src/api/crm/message'
import {defineStore, mapStores} from "pinia";
import {usePermissionStore} from "./permission.js";

/**
 * 消息记录
 */
export const useCrmStore = defineStore('crm', {
  state: () => ({
    // 待办事项消息
    messageNum: {
      todayCustomer: 0,
      followLeads: 0,
      followCustomer: 0,
      checkContract: 0,
      checkReceivables: 0,
      remindReceivablesPlan: 0,
      endContract: 0,
      totalNum: 0
    }
  }),
  actions: {
    /**
     * 更改待办事项
     */
    SET_MESSAGENUM(messageNum) {
      let totalNum = 0
      for (const key in messageNum) {
        if (key != 'totalNum') {
          totalNum += (messageNum[key] || 0)
        }
      }
      messageNum.totalNum = totalNum
      this.messageNum = messageNum
    },
    // 登录
    async GetMessageNum() {
      return new Promise((resolve, reject) => {
        crmMessagNumAPI().then(response => {
              this.SET_MESSAGENUM(response.data.data)
              const permissionStore = usePermissionStore();
              permissionStore.SET_CRMROUTERSNUM(this.messageNum.totalNum)
              resolve(response)
            })
            .catch(error => {
              reject(error)
            })
      })
    }
  }
})
